$(function () {
    wzERP.Common.singleDate('#applystart');  //联动日期搜索日期
})
var vm = new Vue({
    el: '#main',
    data: {
        accountTab: true,//是否显示账号设置tab,否则显示结算方式
        isCreateAccount: false,//是否是添加账号设置
        isCreatePayMethod: false,//是否是添加结算方式
        accountDialogTitle: '',//添加/更新账号的标题
        payMethodDialogTitle: '',//添加/更新结算方式的标题
        accountSettingTable: {},//账户设置列表对象
        payMethodTable: [],//结算方式列表
        accountType: [],//账户类型
        accountCurrency: [],//账户货币
        accountSettingObj: {
            id: '',
            accountName: '',
            paymentTypeId: '0',
            currencyId: '0',
            initialBalance: '',
            accountBank: '',
            account: '',
            initialDate:''//期初金额
        },
        payMethodObj: {
            id: '',
            settlementCode: '',
            settlementName: '',
            daysPayment: '',
            descript: ''
        },
        modal: {
            accountSettingDetail: false, //控制账户设置详情弹窗
            addAccountSetting: false,//控制添加编辑账户设置弹窗
            addPayMethod: false//控制添加/编辑结算方式弹窗
        }
    },
    //页面加载之前调用
    mounted: function () {
        var _this = this;
        _this.getAccountSettingTable(1, 999);
        _this.getPayMethodTable();
        //获取账号类型
        axios.get('/finance/erpAccountReceivable/querydictionDetailListByCode?code=accountType').then(function(res){
        	_this.accountType=res.data.data;
        })
        //获取账号货币
        axios.get('/basedata/currency/queryListAjax',{}).then(function (res) {
            _this.accountCurrency = res.data.data;
        })
    },
    // watcher:
    methods: {
        //点击账号设置tab
        accountMode: function () {
            this.accountTab = true;
        },
        //点击结算方式tab
        settlementMode: function () {
            this.accountTab = false;
        },
        //点击添加账户
        clickCreateAccount: function () {
            this.restAccountSettingForm();
            this.modal.addAccountSetting = true;
            this.accountDialogTitle = '添加账户';
            this.isCreateAccount = true;
        },
        //获取账号设置列表数据
        getAccountSettingTable: function (pageIndex, pageSize) {
            pageSize = 999;
            pageIndex = pageIndex || 1;
            var _this = this;
            axios.get('/finance/erpBillSetting/findPageBillSetting?current='+pageIndex+"&pageSize="+pageSize).then(function (res) {
                if(res.data.code==0){
                    _this.accountSettingTable = res.data.data;
                  _this.$nextTick(function () {
                    permissonHandler();
                  });
                }else{
                    layer.msg(res.data.msg,{time:1000})
                }
            })
        },
        //显示详情
        showAccountDetail: function (id) {
            this.restAccountSettingForm()
            this.modal.accountSettingDetail = true;
            this.getAccountDetail(id);
        },
        //获取是否选择
        getPayType:function(id){
          return this.accountSettingObj.paymentTypeId==id;
        },
        //获取账号详情
        getAccountDetail: function (id) {
            var _this = this;
            axios.get('/finance/erpBillSetting/findDetail?id='+id).then(function (res) {
                if (res.data.code == 0) {
                    _this.accountSettingObj = res.data.data;
                    _this.$refs.initialDate.value=_this.accountSettingObj.initialDate && _this.accountSettingObj.initialDate.substr(0,10);
                    $('#paymenttypeid')
                        .dropdown('set value', _this.accountSettingObj.paymentTypeId)
                    $("#currentsel").dropdown('set value',_this.accountSettingObj.currencyId);
                }
            })
        },
        //充值账户设置表单
        restAccountSettingForm:function(){
            this.accountSettingObj.id="";
            this.accountSettingObj.accountName="";
            this.accountSettingObj.paymentTypeId="0";
            this.accountSettingObj.currencyId="0";
            $('#paymenttypeid').dropdown('set value', 0)
            $("#currentsel").dropdown('set value',0);

            this.accountSettingObj.initialBalance="";
            this.$refs.initialDate.value="";
            this.accountSettingObj.accountBank="";
            this.accountSettingObj.account="";
        },
        addAccountSetting: function () {
            var _this = this
            if (this.isCreateAccount) {
                delete _this.accountSettingObj.id;
            }
            if (!_this.accountSettingObj.accountName) {
                layer.msg('请填写账号名称', {time: 1000})
                return;
            }
            if (_this.accountSettingObj.accountName.trim().length > 20) {
                layer.msg('账号名称最多20字符', {time: 1000})
                return;
            }
            if (_this.accountSettingObj.paymentTypeId==0) {
                layer.msg('请选择账号类型', {time: 1000})
                return;
            }
            if (_this.accountSettingObj.currencyId==0) {
                layer.msg('请选择账户货币', {time: 1000})
                return;
            }


            if (!_this.accountSettingObj.initialBalance) {
                layer.msg('请填写期初余额', {time: 1000})
                return;
            }
            if (_this.accountSettingObj.initialBalance<0 || isNaN(_this.accountSettingObj.initialBalance)) {
                layer.msg('期初余额必须是大于0的数字', {time: 1000})
                return;
            }
            var res=new RegExp(/^[0-9]+(.[0-9]{2})?$/);
            if(!res.test(_this.accountSettingObj.initialBalance)){
                layer.msg('期初余额必须是大于0只能有二位小数的的数字', {time: 1000})
                return;
            }

            if (_this.accountSettingObj.initialBalance.length > 10) {
                layer.msg('期初余额必须小于10位数', {time: 1000})
                return;
            }
            var re = new RegExp(/^\d{16,19}$/);
            if(_this.accountSettingObj.accountBank){
                if (!re.test(_this.accountSettingObj.accountBank)) {
                    layer.msg('银行账户必须16-19位数字', {time: 1000})
                    return;
                }
            }

            if(_this.accountSettingObj.account){
                if (_this.accountSettingObj.account.length>20) {
                    layer.msg('开户行长度在20以内', {time: 1000})
                    return;
                }
            }

            _this.accountSettingObj.initialDate=_this.$refs.initialDate.value
            if(!_this.accountSettingObj.initialDate){
                layer.msg('请选择期初日期', {time: 1000})
                return;
            }


            VUE_UTIL.getAxiosInstance().post("/finance/erpBillSetting/addOrUpdateBillSet", VUE_UTIL.buildPostFormData(_this.accountSettingObj))
                .then(function (response) {
                    if (response.data.code == 0) {
                        layer.msg('操作成功', {time: 1000})
                        _this.modal.addAccountSetting = false;
                        _this.isCreateAccount = false;
                        _this.getAccountSettingTable(1,999);
                    } else {
                        layer.msg(response.data.msg, {time: 1000})
                    }
                })

        },
        //删除账号方法
        delAccount: function (id) {
            var _this = this
            layer.confirm('确定删除该账号吗?', function (index) {
                layer.close(index);

                axios.get('/finance/erpBillSetting/delete?id='+id).then(function (res) {
                    if (res.data.code == 0) {
                        layer.msg('刪除成功', {time: 1000})
                    } else {
                        layer.msg(res.data.msg, {time: 1000})
                    }
                    _this.getAccountSettingTable(1, 999);
                })
            });
        },
        //点击编辑
        clickEdit: function (id) {
            this.restAccountSettingForm()
            this.modal.addAccountSetting = true;
            this.accountDialogTitle = '编辑账户';
            this.getAccountDetail(id);
        },
        //获取结算方式
        getPayMethodTable: function () {
            var _this = this
            axios.get('/finance/erpPaymentType/findAll', {}).then(function (res) {
                _this.payMethodTable = res.data.data
            })
        },
        //点击添加结算方式
        clickCreatePayMethod: function () {
            this.payMethodObj.id = '';
            this.modal.addPayMethod = true;
            this.isCreatePayMethod = true;
            this.payMethodDialogTitle = '添加付款方式'
            this.restPayMethodForm();
        },
        //点击编辑结算方式
        clickEditPayMethod: function (id) {
            var _this = this;
            this.modal.addPayMethod = true;
            this.restPayMethodForm();
            this.payMethodDialogTitle = '编辑付款方式'
            this.isCreatePayMethod=false;
            axios.get('/finance/erpPaymentType/findDetail?id='+id).then(function (res) {
                if (res.data.code == 0 && res.data.data) {
                    _this.payMethodObj = res.data.data;
                } else {
                    layer.msg(res.data.msg, {time: 1000})
                }
            })
        },
        //清空结算方式表单
        restPayMethodForm: function () {
            this.payMethodObj.id="";
            this.payMethodObj.settlementCode="";
            this.payMethodObj.settlementName="";
            this.payMethodObj.daysPayment="";
            this.payMethodObj.descript="";
        },
        //创建或更新结算方式
        savePayMethodData: function () {
            var _this = this;
            if (this.isCreatePayMethod) {
                delete _this.payMethodObj.id;
            }
            if(!_this.payMethodObj.settlementCode || _this.payMethodObj.settlementCode.length>20){
                layer.msg("结算代号长度必须在20内",{time:1000})
                return
            }
            var re=new RegExp(/^[A-Za-z0-9]+$/)
            if(!re.test(_this.payMethodObj.settlementCode)){
                layer.msg("结算名称必须在字母或数字",{time:1000})
                return
            }
            if(!_this.payMethodObj.settlementName || _this.payMethodObj.settlementName.length>20){
                layer.msg("结算名称必须在20字内",{time:1000})
                return
            }

            if(_this.payMethodObj.daysPayment<1 || !_this.payMethodObj.daysPayment >100){
                layer.msg("账期为100天以内",{time:1000})
                return
            }
            if(_this.payMethodObj.descript && _this.payMethodObj.descript.length >200){
                layer.msg("备注为200字以内",{time:1000})
                return
            }

            VUE_UTIL.getAxiosInstance().post("/finance/erpPaymentType/addOrUpdate", VUE_UTIL.buildPostFormData(_this.payMethodObj))
                .then(function (response) {
                    _this.isCreatePayMethod = false;
                    if (response.data.code == 0) {
                        layer.msg('操作成功', {time: 1000})
                        _this.modal.addPayMethod = false;
                        _this.getPayMethodTable();
                    } else {
                        layer.msg(response.data.msg, {time: 1000})
                    }
                })
        },
        //删除结算方式
        deletePayMethod: function (id) {
            var _this=this;
            layer.confirm('确定删除该结算方式吗?', function (index) {
                layer.close(index);
                axios.get('/finance/erpPaymentType/delete?id='+id).then(function (res) {
                    if (res.data.code == 0) {
                        layer.msg('刪除成功', {time: 1000})
                        _this.getPayMethodTable();
                    } else {
                        layer.msg(res.data.msg, {time: 1000})
                    }
                })
            })
        }
    }


})